/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rad.Java.MotelPlus.Views;

import forms.frmCheckin;
import forms.frmCheckout;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import rad.Java.MotelPlus.Class.Apt;
import rad.Java.MotelPlus.Class.StateApt;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import rad.Java.MotelPlus.Class.Stay;
import rad.Java.MotelPlus.Class.Timmer;

/**
 * AptViews.java
 *
 * @author Renato ferreira dos santos
 * Created on 22/03/2010, 16:39:31
 */
public class AptView extends JButton implements ActionListener{
    private Apt apt;
    private Stay stay;
    private StateApt estado;
    private frmCheckin dialogo;
    private Timmer t;
    private String _name;
    private long milliSeconds;

    public AptView(Apt apt)
    {
        super();

        this.apt = apt;
        
        addActionListener(this);
    }

    // <editor-fold defaultstate="collapsed" desc="Declaration of functions">
        public StateApt getEstado() {
            return estado;
        }

        public void setEstado(StateApt estado) {
            DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            apt.setStatus(estado);
            this.estado = estado;

            if(estado == StateApt.Ocupado)
            {
                if (stay == null) {
                    try {
                        stay = new Stay();
                        stay.setAptId(apt.getId());

                        if (!stay.getByAptId()) {
                            JOptionPane.showMessageDialog(this, "Erro ao tentar recuperar Estadia:\n" + stay.getErrorMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                        this.milliSeconds = fmt.parse(stay.getInitDateTime()).getTime();
                    } catch (ParseException ex) {
                    Logger.getLogger(AptView.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                        Logger.getLogger(AptView.class.getName()).log(Level.SEVERE, null, ex);
                    }
                 }
            }

            mudarCor();
        }

        public void btnName(String name)
        {
            this.setText(name);
            this._name = name;
            
            this.setHorizontalAlignment(CENTER);
            this.setHorizontalTextPosition(SwingConstants.CENTER);
            this.setVerticalAlignment(SwingConstants.TOP);
            this.setVerticalTextPosition(BOTTOM);
            
        }

        public String getNameApt()
        {
            return _name;
        }

        private void mudarCor(){
           switch (estado) {
              case Livre:
                 setIcon(new ImageIcon("images/livre.png"));
                 if (t != null)
                     t.done();
                 break;
              case Ocupado:
                 t = new Timmer(this);
                 t.setStart(milliSeconds);
                 setIcon(new ImageIcon("images/ocupado.png"));
                 t.start();
                 break;
              case Faxina:
                 setIcon(new ImageIcon("images/faxina.jpg"));
                 if (t != null)
                     t.done();
                 break;
              case Manutenção:
                 setIcon(new ImageIcon("images/manutencao.jpg"));
                 break;
              default:
                 break;
           }
        }


        public void actionPerformed(ActionEvent arg0) {
            if( apt.getStatus().equals(StateApt.Livre))
            {
                stay = new Stay();
                milliSeconds = System.currentTimeMillis();
                setEstado(new frmCheckin(milliSeconds/1000, apt, stay).showForm());
            }
            else if( apt.getStatus().equals(StateApt.Ocupado))
            {
                try {
                    setEstado(new frmCheckout(apt, stay).showForm());
                } catch (SQLException ex) {
                    Logger.getLogger(AptView.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            else if( apt.getStatus().equals(StateApt.Faxina))
            {
                int resp= JOptionPane.showConfirmDialog(this,
                                              "Deseja mudar o status do apartamento para 'Livre'?",
                                              "Confirmação",
                                              JOptionPane.YES_NO_CANCEL_OPTION,
                                              JOptionPane.QUESTION_MESSAGE);
                if( resp == JOptionPane.YES_OPTION)
                {
                    apt.setStatus(StateApt.Livre);
                    setEstado(StateApt.Livre);

                    if(!apt.update())
                    {
                        JOptionPane.showMessageDialog(this,
                                                      "Erro ao tentar gravar status!\n"+apt.getErrorMessage(),
                                                      "Erro",
                                                      JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        }
        // </editor-fold>
}
